/**
 * Created by Administrator on 2016/5/14.
 */
// 标记当前为测试环境
process.env.NODE_ENV = 'test';

const request = require('supertest');
const app = require('./app');

// 每次执行测试前，刷新数据库，并且选择test作为数据库
const redis = require('redis');
var client = redis.createClient();
client.select('test'.length);
client.flushdb();

describe('请求根目录测试', function () {
    it('返回200状态码，表示请求成功', function (done) {
        request(app)
            .get('/')
            .expect(200, done);
    });
    it('测试返回的是HTML格式数据', function (done) {
        request(app)
            .get('/')
            .expect('Content-Type', /html/, done);
    });
    it("测试返回一个包含'中国城市'四个字的index文件", function (done) {
        request(app)
            .get('/')
            .expect(/中国城市/, done);
    });
});

describe('测试列出所有的city数据', function () {
    it('向 /cities 发出请求', function (done) {
        request(app)
            .get('/cities')
            .expect(200, done);
    });
    it('数据格式为json', function (done) {
        request(app)
            .get('/cities')
            .expect('Content-Type', /json/, done);
    });
    it('测试初始化redis，应该返回空数组', function (done) {
        request(app)
            .get('/cities')
            .expect(JSON.stringify([]), done);
    });
});

describe("创建城市信息", function () {
    it("返回的HTML代码应该是201，返回的JSON数据中应该包含杭州两字", function (done) {
        request(app)
            .post('/cities')
            .send('name=HangZhou&desc=西施的故乡')
            .expect(201)
            .expect(/HangZhou/, done);
    });

    it("校验输入的name和desc不能为空", function (done) {
        request(app)
            .post('/cities')
            .send('name=&desc=')
            .expect(400, done);
    });
});

describe("删除城市信息", function () {
    before(function () {
        client.hset('cities', 'city_del', 'test delete');
    });

    after(function () {
        client.hdel('cities', 'city_del');
    });

    it("删除名为city_del的城市", function (done) {
        request(app)
            .delete('/cities/city_del')
            .expect(204, done);
    });
});

describe("使用EJS生成的template显示city的desc信息", function () {
    before(function () {
        client.hset('cities', 'ejs_test', 'ejs_desc');
    });

    after(function () {
        client.hdel('cities', 'ejs_test');
    });

    it("html代码为200", function (done) {
        request(app)
            .get('/cities/ejs_test')
            .expect(200, done);
    });

    it("返回数据格式为html", function (done) {
        request(app)
            .get('/cities/ejs_test')
            .expect('Content-Type', /html/, done);
    });

    it("返回的文件中包含ejs_desc", function (done) {
       request(app)
           .get('/cities/ejs_test')
           .expect(/ejs_desc/, done);
    });
});


